#include "tablewidget.h"

tablewidget::tablewidget(QWidget *parent) : QListWidget(parent)
{
    setAcceptDrops(true);
}
void tablewidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        pressed = true;
        startPos = event->pos();          // 记录开始位置
        startSize = size();        // 记录开始大小
        //确定光标
        const double resizeHandleSize=10;
        bool right=abs(startPos.x()-rect().right())<=resizeHandleSize;
        bool bottom=abs(startPos.y()-rect().bottom())<=resizeHandleSize;
        if (right&&bottom){
            selectedge = rbedge;
            setCursor(Qt::SizeFDiagCursor);
        }
        else if(bottom){
            selectedge=bedge;
            setCursor(Qt::SizeVerCursor);
        }
        else if(right){
            selectedge=redge;
            setCursor(Qt::SizeHorCursor);
        }
        else {
            selectedge=body;
            setCursor(Qt::SizeAllCursor);
        }
    }
}

void tablewidget::mouseMoveEvent(QMouseEvent *event)
{
    if (pressed) {
        // 判断点击的位置
        QPointF diff = event->pos() - startPos;
        QSize newSize =startSize + QSize(diff.x(), diff.y());
        if (selectedge ==body){
            QPoint endPos = event->pos();
            QPoint offset = endPos - startPos;
            move(pos() + offset);     // 移动控件
        }
        else{
            if(selectedge==redge){
                if(newSize.width()>7)
                    resize(newSize.width(),startSize.height());
            }
            else if(selectedge==bedge){
                if(newSize.height()>10)
                    resize(startSize.width(),newSize.height());
            }
            else if(selectedge == rbedge){

                if(newSize.width()>7&&newSize.height()>10)
                    resize(newSize);
            }

        }
    }
}

void tablewidget::mouseReleaseEvent(QMouseEvent *event)
{
    pressed = false;
    selectedge=0;
}

void tablewidget::resizeEvent(QResizeEvent *event)
{
    QListWidget::resizeEvent(event);
    emit widgetResized(size(), startSize);  // 发出大小变化信号
}
